package com.lance.answer.gui.answer.gk.answer;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.DealInfoUtil;
import com.lance.answer.gui.answer.gk.answer.task.UploadFileTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingDeque;

@Slf4j
public class UploadFileTmp2 {

    /**
     * 进度表：
     * 6-18 19:55开始测试 公共行政学 行政管理专科
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
    //    Thread.sleep(1000*60*60*4);
        int sync = 8;
      //  Thread.sleep(1000*60*60*6);
       // Thread.sleep(1000*60*60*9);
        Map<String, Set<String>> uploadMap = Maps.newHashMap();
        for(File file:new File("libs//courseUrlInfo").listFiles()){

            Set<String> forums = Sets.newHashSet();
            List<String> urlList = IOUtils.readLines(new FileInputStream(file),"UTF-8");
            for(String url:urlList){
                if(url.contains("assign")){
                    //     if(url.contains("形考")||url.contains("作业")){
                    if(url.contains("形考")
                            ||url.contains("测验")
                            ||url.contains("实训")
                            ||url.contains("考核")
                            ||url.contains("考试")
                            ||url.contains("作业") // add 2022-6-12
                            ||url.contains("实验报告")
                            ||url.contains("记分作业")
                            ||url.contains("需要批阅")
                            ||url.contains("实验")
                            ||url.contains("任务检测")
                            ||url.contains("社会实践")
                    ){
                      if(url.contains(",")){
                          forums.add(url.split(",")[0]);
                      }else {
                          forums.add(url);
                      }
                    }
                }
            }
            String name = file.getName();
            name = name.substring(0,name.length()-4);
            log.info("name:{}",name);
            uploadMap.put(name,forums);
        }

//        for(String subjectName:uploadMap.keySet()){
//          if(uploadMap.get(subjectName)!=null&&uploadMap.get(subjectName).size()>0){
//              log.info("subjectName:{}，value:{}",subjectName, JSON.toJSONString(uploadMap.get(subjectName)));
//          }
//
//        }
//
//        Thread.sleep(3000000);

        LinkedBlockingDeque<AnswerDTO> answerQueue = new LinkedBlockingDeque<>();

        // 获取所有的用户名和密码



        List<String> teachers = Lists.newArrayList();

        teachers.add("刘庆");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("刘");
        teachers.add("李明");
        teachers.add("朱晓玉");
        teachers.add("宋柳萌");
        teachers.add("零散");

        for(String teacher:teachers){
            Map<String, String> usernameAndPwd = Maps.newHashMap();
            InputStream userInputStream = new FileInputStream("libs//teachers//"+teacher+".user");
            List<String> userString = IOUtils.readLines(userInputStream, "utf-8");
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[0];
                String pwd = "";
                if (strs[1].length() < 18) {
                    pwd = strs[1];
                } else {
                    pwd = strs[1].substring(6, 14);
                    pwd = "Ouchn" + pwd;
                }
                usernameAndPwd.put(userCode, pwd);
            }
            for(String subjectName:uploadMap.keySet()){
                if(uploadMap.get(subjectName)==null||uploadMap.get(subjectName).size()==0){
                    System.err.println("subjectNamesubjectNamesubjectName:"+subjectName);
                    continue;
                }
                //  LinkedList<AnswerRequestDTO> requestUrl = Lists.newLinkedList();
                LinkedList<String> requestUrl = Lists.newLinkedList(uploadMap.get(subjectName));

                log.info("subjectName:{}",subjectName);

                // 根据课程名称拿到 所有人 这里的用户只有用户名，没有密码
                if(subjectName.contains("机械CAD")){
                    subjectName = "机械CAD/CAM";
                }
                List<String> userNames = DealInfoUtil.countUserCourseInfo("libs//userCourseInfo//" + teacher + "//").get(subjectName);
                if(userNames == null){
                    continue;
                }
                for(String username:userNames){
//                    if(!Util.getVip().contains(username)){
//                        continue;
//                    }
                    String pwd = usernameAndPwd.get(username);
                    AnswerDTO answerDTO = new AnswerDTO();
                    if(subjectName.contains("/")){
                        subjectName =   subjectName.replaceAll("/","");
                    }
                    answerDTO.setSubjectName(subjectName);
                    answerDTO.setUsername(username);
                    answerDTO.setPwd(pwd);
                    answerDTO.setCommit(true);
                    answerDTO.setPush(false);
                    answerDTO.setUploadUrl(requestUrl);
                    answerDTO.setTeacher(teacher);
//                if(!answerDTO.getSubjectName().contains("机械CAD")){
//                    continue;
//                }
//                                   if(
//                       !subjectName.equals("习近平新时代中国特色社会主义思想")
//                       &&!subjectName.equals("思想道德修养与法律基础")
//                               &&!subjectName.equals("形势与政策")
//                               &&!subjectName.equals("思想道德与法治")
//                               &&!subjectName.equals("毛泽东思想和中国特色社会主义理论体系概论")
//                               &&!subjectName.equals("中国近现代史纲要")
//                               &&!subjectName.equals("马克思主义基本原理概论")
//               ){
//                   continue;
//               }
//                    if (
//                            subjectName.equals("习近平新时代中国特色社会主义思想")
//                                    || subjectName.equals("思想道德修养与法律基础")
//                                    || subjectName.equals("形势与政策")
//                                    || subjectName.equals("思想道德与法治")
//                                    || subjectName.equals("毛泽东思想和中国特色社会主义理论体系概论")
//                                    || subjectName.equals("中国近现代史纲要")
//                                    || subjectName.equals("马克思主义基本原理概论")
//                    ) {
//                        continue;
//                    }
//                    if(
//                            !subjectName.equals("儿童发展问题的咨询与辅导")
//                    ){
//                        continue;
//                    }
                    answerQueue.add(answerDTO);
                }

            }
        }



        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new UploadFileTask(null,answerQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName( "答题线程--" + i);
            answerThread.start();
        }


    }






}
